package com.resource.manager.search;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import com.resource.manager.search.vo.FileEntity;
import com.resource.manager.search.vo.FileType;
import com.resource.manager.search.vo.FileTypeEntity;
import com.resource.manager.search.vo.MainSearchEntity;
import com.resource.manager.ui.CustButton;
import com.resource.manager.ui.CustTextField;
import com.resource.manager.ui.FileToClipboard;
import com.resource.manager.ui.ImagePanel;
import com.resource.manager.util.BeanCopyUtil;
import com.resource.manager.util.FileSearchUtil;
import com.resource.manager.util.SwingUtil;
import lombok.SneakyThrows;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

import static com.resource.manager.search.MainSearchWindow.RightPanel.*;

/**
 * 查询主窗口
 */
public class MainSearchWindow extends JFrame {

    private MainSearchEntity entity;
    private RightPanel right;
    private LeftPanel left;

    /**
     * 创建主窗口
     * @param entity 查询实体
     */
    @SneakyThrows
    public MainSearchWindow(MainSearchEntity entity) {
        this.entity = entity;

        setTitle("资源查询");
        SwingUtil.setTitleIco(this);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //设置边框
//        setResizable(false);

        //获取屏幕大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double maxWidth = screenSize.getWidth();
        double maxHeight = screenSize.getHeight();
        //设置窗口大小为屏幕大小一半,居中显示
        setSize((int) (maxWidth), (int) (maxHeight));
        setLocationRelativeTo(null);
        setLayout(null);
        //设置背景色为白色
        getContentPane().setBackground(Color.WHITE);

        JPanel topPanel = new JPanel();
        topPanel.setBounds(0, 0, (int) maxWidth, 60);
        topPanel.setLayout(null);
        //设置顶部边框为亮灰色
        topPanel.setBackground(Color.WHITE);
        this.add(topPanel);


        right = new RightPanel(entity, (int) maxWidth - 300, (int) maxHeight - 120);
        right.searchContentPanel.fileEntities = entity.getFileTypeEntitys().get(0).getFileList();

        // 创建两个面板作为分区内容
        left = new LeftPanel(entity, right);


        // 创建水平方向的JSplitPane
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
        splitPane.setBackground(Color.WHITE);
        // 设置分隔条的位置
        splitPane.setDividerLocation(300);
        splitPane.setBounds(0, 60, (int) maxWidth, (int) maxHeight - 120);
        splitPane.setDividerSize(3);
        //设置分割线边框无色
        splitPane.setBorder(null);


        // 将JSplitPane添加到窗口中
        this.add(splitPane);


        initTopComponent(topPanel);
        this.setVisible(true);


        //监听窗口缩放
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                topPanel.setBounds(0, 0, getWidth(), 60);
                topPanel.revalidate();
                topPanel.repaint();

                splitPane.setBounds(0, 60, getWidth(), getHeight() - 120); // 更新
                splitPane.revalidate();
                splitPane.repaint();

                left.revalidate();
                left.repaint();

                right.revalidate();
                left.repaint();
                //组件大小调整,自动刷新
//                left.setBounds(0,0,left.getWidth(),splitPane.getHeight());
//                right.setBounds(left.getWidth(),0,getWidth() - left.getWidth(),splitPane.getHeight());

            }
        });
    }

    private void initTopComponent(JPanel topPanel){

        CustTextField searchField = new CustTextField();
        searchField.setBounds(20, 13, 300, 35);
        this.setFont(new java.awt.Font("微软雅黑", Font.PLAIN, 16));
        //美化文本框
        topPanel.add(searchField);

        CustButton searchButton = new CustButton("搜索");
        searchButton.setBounds(330, 13, 100, 35);
        searchButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        searchButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                ImagePanel.cache.clear();
                String text = searchField.getText();
                if(ObjectUtil.isEmpty(text)){
                    entity.setFileTypeEntitys(entity.getAllFileTypeEntitys());
                    left.initLeftPanel();
                    left.revalidate();
                    left.repaint();
                    FileTypeEntity typeEntity = entity.getFileTypeEntitys().stream().filter(f -> Boolean.TRUE.equals(f.isSCheckFlag())).findFirst().orElse(null);
                    if(!ObjectUtil.isEmpty(typeEntity)){
                        right.searchContentPanel.fileEntities = typeEntity.getFileList();
                    }
                    right.searchContentPanel.initContentPanel();
                    right.searchContentPanel.revalidate();
                    right.searchContentPanel.repaint();
                    right.revalidate();
                    right.repaint();
                }else{
                    List<FileTypeEntity> allFileTypeEntitys = entity.getAllFileTypeEntitys();
                    //超级搜索过滤
                    List<FileTypeEntity> fileTypeEntitys = new ArrayList<>();
                    for (FileTypeEntity fileTypeEntity : allFileTypeEntitys) {
                        List<FileEntity> fileEntities = new ArrayList<>();
                        for (FileEntity fileEntity : fileTypeEntity.getFileList()) {
                            if(fileEntity.getFileName().toUpperCase().contains(text.toUpperCase())){
                                fileEntities.add(fileEntity);
                            }
                        }
                        FileTypeEntity typeEntity = BeanCopyUtil.copy(fileTypeEntity, FileTypeEntity.class);
                        typeEntity.setFileList(fileEntities);
                        typeEntity.setCount(fileEntities.size());
                        fileTypeEntitys.add(typeEntity);
                    }
                    entity.setFileTypeEntitys(fileTypeEntitys);
                    left.initLeftPanel();
                    left.revalidate();
                    left.repaint();
                    FileTypeEntity typeEntity = entity.getFileTypeEntitys().stream().filter(f -> Boolean.TRUE.equals(f.isSCheckFlag())).findFirst().orElse(null);
                    if(!ObjectUtil.isEmpty(typeEntity)){
                        right.searchContentPanel.fileEntities = typeEntity.getFileList();
                    }
                    right.searchContentPanel.pageIndex = 1;
                    right.searchContentPanel.initContentPanel();
                    right.searchContentPanel.revalidate();
                    right.searchContentPanel.repaint();
                    right.revalidate();
                    right.repaint();
                }
            }
        });
        topPanel.add(searchButton);

        //导出
        CustButton exportButton = new CustButton("导出");
        exportButton.setBounds(440, 13, 100, 35);
        exportButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        exportButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        MainSearchWindow self = this;
        exportButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                JDialog dialog = new JDialog(self);
                dialog.setTitle("导出设置");
                SwingUtil.setTitleIco(dialog);
                dialog.setResizable(false);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setSize(400, 150 + entity.getFileTypeEntitys().size() * 40);
                //居中显示
                dialog.setLocationRelativeTo(null);
                dialog.setLayout(null);
                dialog.getContentPane().setBackground(Color.WHITE);
                dialog.setBackground(Color.WHITE);
                dialog.setVisible(true);

                List<FileTypeEntity> typeEntitys = entity.getFileTypeEntitys();
                int y = 10;
                List<FileTypeEntity> exportList = new ArrayList<>();
                Map<JCheckBox, FileTypeEntity> boxList = new HashMap<>();
                for (FileTypeEntity fileType : typeEntitys) {
                    JPanel panel = new JPanel();
                    panel.setBackground(Color.WHITE);
                    panel.setBounds(20, y, 300, 40);
                    panel.setLayout(null);

                    //复选框
                    JCheckBox checkBox = new JCheckBox();
                    checkBox.setBounds(5, 8, 20, 20);
                    checkBox.setBackground(Color.WHITE);
                    checkBox.addActionListener(e1 -> {
                        if (checkBox.isSelected()) {
                            if (!exportList.stream().anyMatch(e2 -> e2.getName().equals(fileType.getName()))) {
                                //不存在的情况下,添加
                                exportList.add(fileType);
                            }
                        } else {
                            exportList.removeIf(e2 -> e2.getName().equals(fileType.getName()));
                        }
                    });
                    boxList.put(checkBox,fileType);
                    panel.add(checkBox);

                    ImagePanel imagePanel = new ImagePanel(fileType.getIco());
                    imagePanel.setBounds(40, 5, 30, 30);
                    panel.add(imagePanel);
                    JLabel label = new JLabel(fileType.getName());
                    label.setBounds(100, 5, 180, 30);
                    label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
                    panel.add(label);

                    y += 40;
                    dialog.add(panel);
                }
                //复选框
                JCheckBox allCheck = new JCheckBox();
                allCheck.setBounds(25, y + 10, 20, 20);
                allCheck.setBackground(Color.WHITE);
                boolean exitNoCheck = boxList.keySet().stream().anyMatch(b -> !b.isSelected());
                if (!exitNoCheck) {
                    allCheck.setSelected(true);
                }
                allCheck.addActionListener(e1 -> {
                    if (allCheck.isSelected()) {
                        boxList.forEach((k, v) -> {
                            k.setSelected(true);
                            if (!exportList.stream().anyMatch(e2 -> e2.getName().equals(v.getName()))) {
                                //不存在的情况下,添加
                                exportList.add(v);
                            }
                        });
                    } else {
                        boxList.forEach((k, v) -> {
                            k.setSelected(false);
                            exportList.clear();
                        });
                    }
                });
                dialog.add(allCheck);
                JLabel label = new JLabel("全选");
                label.setBounds(60, y + 5, 100, 30);
                label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
                dialog.add(label);

                //添加path
                JLabel exportPath = new JLabel("");
                //根据字体像素长度设置label宽度
                //根据font像素获取字体占用长度
                int textWidth = SwingUtil.getFontWidth(getGraphics(), new Font("微软雅黑", Font.PLAIN, 12), exportPath.getText());
                exportPath.setBounds( dialog.getWidth() - 50 - textWidth, y + 30, textWidth, 30);
                exportPath.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                dialog.add(exportPath);


                CustButton selectDir = new CustButton("导出目录");
                selectDir.setBounds(160, y + 60, 100, 30);
                int i = y + 30;
                selectDir.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        JFileChooser chooser = new JFileChooser();
                        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                        chooser.showDialog(new JLabel(), "选择文件夹");
                        if (chooser.getSelectedFile() != null) {
                            exportPath.setText(chooser.getSelectedFile().getAbsolutePath());
                            int newTextWidth = SwingUtil.getFontWidth(getGraphics(), new Font("微软雅黑", Font.PLAIN, 12), exportPath.getText());
                            exportPath.setBounds((dialog.getWidth() - 50 - newTextWidth), i, newTextWidth, 30);
                            System.out.println("选择文件夹: " + chooser.getSelectedFile().getAbsolutePath());
                        }
                    }
                });
                dialog.add(selectDir);

                CustButton confirm = new CustButton("确定");
                confirm.setBounds(280, y + 60, 80, 30);
                confirm.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(ObjectUtil.isEmpty(exportList)){
                            JOptionPane.showMessageDialog(null, "请选择需要导出的文件类型");
                            return;
                        }
                        String path = exportPath.getText();
                        if(ObjectUtil.isEmpty(path)){
                            JOptionPane.showMessageDialog(null, "请选择导出目录");
                            return;
                        }
                        //导出
                        FileSearchUtil.export(exportList,path);
                        //设置可操作父窗口
                        dialog.dispose();
                    }
                });
                dialog.add(confirm);
            }
        });
        topPanel.add(exportButton);
    }

    private static class LeftPanel extends JPanel {

        private MainSearchEntity entity;
        private List<JPanel> fileTypePanels = new ArrayList<>();
        private RightPanel right;
        private JPanel panel;
        private JScrollPane scroll;

        public LeftPanel(MainSearchEntity entity, RightPanel right) {
            this.entity = entity;
            setLayout(null);
            this.right = right;

            //内置容器
            panel = new JPanel();
            panel.setBackground(Color.WHITE);
            panel.setLayout(null);
            panel.setBounds(0, 0, this.getWidth(), this.getHeight());

            scroll = new JScrollPane();
            scroll.setBackground(Color.WHITE);
            scroll.setBounds(0, 0, this.getWidth(), this.getHeight());
            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scroll.getVerticalScrollBar().setUnitIncrement(30); //

            scroll.setViewportView(panel);
            //设置滚动条可滚动
            scroll.setEnabled(true);
            this.add(scroll);

            initLeftPanel();

            //监听窗口缩放
            this.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentResized(ComponentEvent e) {
                    scroll.setBounds(0, 0, getWidth(), getHeight());
                    panel.setBounds(0, 0, getWidth(), getHeight());
                    initLeftPanel();
                    repaint();
                }
            });
            setVisible(true);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
        }

        private void initLeftPanel() {
            entity.calSLocal(panel.getWidth());
            List<FileTypeEntity> typeEntitys = entity.getFileTypeEntitys();

            fileTypePanels.forEach(panel::remove);
            fileTypePanels.clear();

            int height = panel.getHeight();
            int maxHeight = 0;
            for (FileTypeEntity it : typeEntitys) {
                JPanel fileTypePanel = new JPanel();
                fileTypePanel.setName(it.getId());
                fileTypePanel.setBounds(it.getSX(), it.getSY(), it.getSWidth() - 40, it.getSHeight());
                fileTypePanel.setLayout(null);
//                fileTypePanel.setBackground(Color.RED);
                fileTypePanel.setVisible(true);

                ImagePanel imgP = new ImagePanel(it.getIco());
                imgP.setBounds(10, 5, 50, 50);
                imgP.setVisible(true);
                fileTypePanel.add(imgP);

                JLabel nameLabel = new JLabel(it.getName());
                nameLabel.setBounds(100, 10, 200, 20);
                nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
                fileTypePanel.add(nameLabel);

                JLabel countLabel = new JLabel("数量：" + it.getCount());
                countLabel.setBounds(100, 30, 200, 20);
                //设置font
                countLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
                fileTypePanel.add(countLabel);
                if (Boolean.TRUE.equals(it.isSCheckFlag())) {
                    fileTypePanel.setBackground(new Color(233, 237, 250));
                } else {
                    fileTypePanel.setBackground(Color.WHITE);
                }

                fileTypePanel.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        super.mousePressed(e);
                        if (Boolean.TRUE.equals(it.isSCheckFlag())) {
                            return;
                        }
                        entity.setSCheckFlagTrue(it);
                        changeFilePanelBackground();
                        right.searchField.setText("");
                        right.searchContentPanel.fileEntities = it.getFileList();
                        //刷新查询内容
                        right.searchContentPanel.pageIndex = 1;
                        right.searchContentPanel.initContentPanel();
                        right.searchContentPanel.repaint();
                        right.searchContentPanel.revalidate();
                        right.repaint();
                    }

                    @Override
                    public void mouseWheelMoved(MouseWheelEvent e) {
                        super.mouseWheelMoved(e);
                    }

                    @Override
                    public void mouseDragged(MouseEvent e) {
                        super.mouseDragged(e);
                    }

                    @Override
                    public void mouseMoved(MouseEvent e) {
                        super.mouseMoved(e);
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        super.mouseReleased(e);
                    }
                });
                fileTypePanels.add(fileTypePanel);

                maxHeight = maxHeight + it.getSHeight() + 10;
                panel.add(fileTypePanel);
            }
            if (maxHeight > height && height > 0) {
                panel.setPreferredSize(new Dimension(panel.getWidth(), maxHeight + 20));
                scroll.repaint();
                repaint();
            }

        }

        private void changeFilePanelBackground() {
            FileTypeEntity typeEntity = entity.getFileTypeEntitys().stream().filter(f -> Boolean.TRUE.equals(f.isSCheckFlag())).findFirst().orElse(null);
            if (!ObjectUtil.isEmpty(typeEntity)) {
                fileTypePanels.forEach(filePanel -> {
                    if (filePanel.getName().equals(typeEntity.getId())) {
                        filePanel.setBackground(new Color(233, 237, 250));
                    } else {
                        filePanel.setBackground(Color.WHITE);
                    }
                });
            }
        }
    }

    public static class RightPanel extends JPanel {

        private MainSearchEntity entity;

        //下拉框
        private JComboBox<String> searchTypeComboBox;
        private JComboBox<String> sortComboBox;
        private CustTextField searchField;
        private CustButton searchButton;
        private SearchContentPanel searchContentPanel;
        private String searchType;
        private String sortType;

        public static final String TIME_DESC = "时间倒序";
        public static final String TIME_ASC = "时间正序";
        public static final String STOR_DESC = "大小倒序";
        public static final String STOR_ASC = "大小正序";

        public RightPanel(MainSearchEntity entity, int width, int height) {
            this.setBackground(Color.WHITE);
            this.setLayout(null);
            this.entity = entity;

            JPanel top = new JPanel();
            top.setLayout(null);
            top.setBackground(Color.WHITE);
            //设置border
            top.setBorder(new LineBorder(Color.lightGray, 1));

            searchContentPanel = new SearchContentPanel(entity, this);
            searchContentPanel.setBackground(Color.WHITE);
            searchContentPanel.setVisible(true);


            // 创建水平方向的JSplitPane
            JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, top, searchContentPanel);
            splitPane.setBackground(Color.WHITE);
            // 设置分隔条的位置
            splitPane.setDividerLocation(60);
            splitPane.setBounds(getX(), getY(), width, height);
            splitPane.setDividerSize(1);
            splitPane.setBorder(null);
            splitPane.setEnabled(false);
            this.add(splitPane);
            this.setVisible(true);

            loadTopPanel(top);
            //监听窗口缩放
            this.addComponentListener(new ComponentAdapter() {
                @Override
                public void componentResized(ComponentEvent e) {
                    splitPane.setBounds(0, 0, getWidth(), getHeight());
                    splitPane.revalidate();
                    splitPane.repaint();

                    top.setBounds(0, 0, getWidth(), 60);
                    top.revalidate();
                    top.repaint();


                    searchContentPanel.setBounds(0, 60, getWidth(), getHeight() - 60);
                    searchContentPanel.revalidate();
                    searchContentPanel.repaint();
                    repaint();
                    loadTopPanel(top);
                    searchContentPanel.initContentPanel();
                }
            });
        }

        private void loadTopPanel(JPanel top) {
            if (!ObjectUtil.isEmpty(searchField)) {
                top.remove(searchField);
            }
            if (!ObjectUtil.isEmpty(searchButton)) {
                top.remove(searchButton);
            }
            if (!ObjectUtil.isEmpty(searchTypeComboBox)) {
                top.remove(searchTypeComboBox);
            }
            if(!ObjectUtil.isEmpty(sortComboBox)){
                top.remove(sortComboBox);
            }
            //添加排序方式, 大小排序,时间排序
            sortComboBox = new JComboBox<>();
            sortComboBox.setBounds(top.getWidth() - 700, 13, 100, 35);
            sortComboBox.addItem(TIME_DESC);
            sortComboBox.addItem(TIME_ASC);
            sortComboBox.addItem(STOR_DESC);
            sortComboBox.addItem(STOR_ASC);
            if (sortType == null) {
                sortType = TIME_DESC;
            }
            //设置默认选项为searchType
            sortComboBox.setSelectedItem(sortType);
            sortComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            sortComboBox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JComboBox source = (JComboBox) e.getSource();
                    Object selectedItem = source.getSelectedItem();
                    sortType = selectedItem.toString();
                    searchContentPanel.initContentPanel();
                    searchContentPanel.revalidate();
                    searchContentPanel.repaint();
                }
            });
            top.add(sortComboBox);

            //添加查看方式下拉框  有大图标,详细信息两个选项
            searchTypeComboBox = new JComboBox<>();
            searchTypeComboBox.setBounds(top.getWidth() - 600, 13, 100, 35);
            searchTypeComboBox.addItem("详细信息");
            searchTypeComboBox.addItem("大图标");
            if (searchType == null) {
                searchType = "详细信息";
            }
            //设置默认选项为searchType
            searchTypeComboBox.setSelectedItem(searchType);
            searchTypeComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            searchTypeComboBox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JComboBox source = (JComboBox) e.getSource();
                    Object selectedItem = source.getSelectedItem();
                    searchType = selectedItem.toString();
                    searchContentPanel.initContentPanel();
                    searchContentPanel.revalidate();
                    searchContentPanel.repaint();
                }
            });
            top.add(searchTypeComboBox);

            searchField = new CustTextField();
            searchField.setBounds(top.getWidth() - 500, 13, 300, 35);
            this.setFont(new java.awt.Font("微软雅黑", Font.PLAIN, 16));
            //美化文本框
            top.add(searchField);

            searchButton = new CustButton("搜索");
            searchButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            searchButton.setBounds(top.getWidth() - 180, 13, 100, 35);
            searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
            searchButton.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    FileTypeEntity typeEntity = entity.getFileTypeEntitys().stream().filter(f -> Boolean.TRUE.equals(f.isSCheckFlag())).findFirst().orElse(null);
                    String text = searchField.getText();
                    if (ObjectUtil.isEmpty(text)) {
                        if (!ObjectUtil.isEmpty(typeEntity)) {
                            searchContentPanel.fileEntities = typeEntity.getFileList();
                            searchContentPanel.initContentPanel();
                            searchContentPanel.revalidate();
                        }
                        return;
                    }
                    if (!ObjectUtil.isEmpty(typeEntity)) {
                        List<FileEntity> fileList = typeEntity.getFileList();
                        List<FileEntity> list = fileList.stream().filter(f -> f.getFileName().toUpperCase().contains(text.toUpperCase())).toList();
                        searchContentPanel.fileEntities = list;
                        searchContentPanel.initContentPanel();
                        searchContentPanel.revalidate();
                        searchContentPanel.repaint();
                    }

                }
            });
            top.add(searchButton);


        }

    }

    private static class SearchContentPanel extends JPanel {

        private MainSearchEntity entity;
        private List<JPanel> contentPanels = new ArrayList<>();
        private int pageIndex = 1;
        private int pageSize = 20;
        private JScrollPane scroll;
        private JPanel contentPanel;
        private JPanel pagePanel;
        private List<FileEntity> fileEntities = new ArrayList<>();
        private JLabel count;
        private JLabel next;
        private JLabel before;
        private JLabel nowPage;
        private RightPanel rightPanel;

        public SearchContentPanel(MainSearchEntity entity, RightPanel right) {
            this.entity = entity;
            this.rightPanel = right;
            this.setLayout(null);
            this.setBackground(Color.WHITE);

            initContentPanel();
            this.setVisible(true);
        }

        protected void initContentPanel() {
            if (contentPanel != null) {
                contentPanels.forEach(panel -> {
                    contentPanel.remove(panel);
                });
                this.remove(contentPanel);
            }
            if (scroll != null) {
                this.remove(scroll);
            }
            if (pagePanel != null) {
                if (count != null) {
                    pagePanel.remove(count);
                }
                if (next != null) {
                    pagePanel.remove(next);
                }
                if (before != null) {
                    pagePanel.remove(before);
                }
                if (nowPage != null) {
                    pagePanel.remove(nowPage);
                }
                this.remove(pagePanel);
            }
            contentPanel = new JPanel();
            contentPanel.setLayout(null);
            contentPanel.setBackground(Color.WHITE);
            contentPanel.setBounds(0, 0, this.getWidth() - 20, this.getHeight() - 60);
            initContentChildPanel(contentPanel);
            contentPanel.setVisible(true);

            scroll = new JScrollPane();
            scroll.setBackground(Color.WHITE);
            scroll.setBounds(0, 0, this.getWidth() - 10, this.getHeight() - 60);
            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scroll.getVerticalScrollBar().setUnitIncrement(30); //

            scroll.setViewportView(contentPanel);
            //设置滚动条可滚动
            scroll.setEnabled(true);
            scroll.setVisible(true);
            this.add(scroll);


            pagePanel = new JPanel();
            pagePanel.setBounds(0, this.getHeight() - 80, this.getWidth(), 60);
            pagePanel.setLayout(null);
            pagePanel.setBackground(Color.white);
            pagePanel.setVisible(true);

            count = new JLabel("共" + fileEntities.size() + "条");
            count.setBounds(this.getWidth() - 400, 30, 100, 20);
            count.setFont(new Font("微软雅黑", Font.PLAIN, 13));
            count.setForeground(Color.GRAY);
            pagePanel.add(count);

            nowPage = new JLabel("第" + pageIndex + "页");
            nowPage.setBounds(this.getWidth() - 280, 30, 100, 20);
            nowPage.setForeground(Color.GRAY);
            nowPage.setFont(new Font("微软雅黑", Font.PLAIN, 13));
            pagePanel.add(nowPage);

            before = new JLabel("上一页");
            before.setBounds(getWidth() - 200, 30, 60, 20);
            //设置字体颜色为蓝色
            before.setForeground(Color.BLUE);
            before.setCursor(new Cursor(Cursor.HAND_CURSOR));
            before.setFont(new Font("微软雅黑", Font.PLAIN, 15));
            before.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    if (pageIndex > 1) {
                        pageIndex--;
                        initContentPanel();
                        revalidate();
                        repaint();
                    }
                }
            });
            pagePanel.add(before);


            next = new JLabel("下一页");
            next.setBounds(getWidth() - 120, 30, 60, 20);
            next.setFont(new Font("微软雅黑", Font.PLAIN, 15));
            next.setForeground(Color.BLUE);
            next.setCursor(new Cursor(Cursor.HAND_CURSOR));
            next.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    if ((pageIndex < (fileEntities.size() / pageSize)) ||
                            ((pageIndex == (fileEntities.size() / pageSize)) && (fileEntities.size() % pageSize != 0))) {
                        pageIndex++;
                        initContentPanel();
                        revalidate();
                        repaint();
                    }
                }
            });
            pagePanel.add(next);

            this.add(pagePanel);
        }

        private void initContentChildPanel(JPanel contentPanel) {
            contentPanels.forEach(contentPanel::remove);
            contentPanels.clear();

            List<FileTypeEntity> fileTypeEntitys = entity.getFileTypeEntitys();
            FileTypeEntity typeEntity = fileTypeEntitys.stream().filter(f -> Boolean.TRUE.equals(f.isSCheckFlag())).findFirst().orElse(null);
            if (typeEntity != null) {
                List<FileEntity> fileList = fileEntities;
                if(TIME_DESC.equals(rightPanel.sortType)){
                    fileList = fileEntities.stream().sorted(Comparator.comparing(FileEntity::getLastModifyTime).reversed()).toList();
                }else if (TIME_ASC.equals(rightPanel.sortType)){
                    fileList = fileEntities.stream().sorted(Comparator.comparing(FileEntity::getLastModifyTime)).toList();
                }else if(STOR_DESC.equals(rightPanel.sortType)){
                    fileList = fileEntities.stream().sorted(Comparator.comparing(FileEntity::getSize).reversed()).toList();
                }else if (STOR_ASC.equals(rightPanel.sortType)){
                    fileList = fileEntities.stream().sorted(Comparator.comparing(FileEntity::getSize)).toList();
                }
                int y = 10;
                int x = 10;
                //根据pageIndex  pageSize进行分页
                int start = (pageIndex - 1) * pageSize;
                int end = Math.min(start + pageSize, fileList.size());
                for (int i = start; i < end; i++) {
                    FileEntity file = fileList.get(i);
                    JPanel childPanel = new JPanel();
                    childPanel.setBackground(Color.WHITE);
                    if ("详细信息".equals(rightPanel.searchType)) {
                        y = contentDetailInit(file, childPanel, y);
                    } else {
                        Pair<Integer, Integer> res = contentBigInit(file, childPanel, y, x);
                        x = res.getKey();
                        y = res.getValue();
                        //最后一个元素
                        if (i == end - 1) {
                            y = y + 210;
                        }
                    }
                }
                if (y > contentPanel.getHeight()) {
                    contentPanel.setPreferredSize(new Dimension(contentPanel.getWidth(), y + 30));
                    contentPanel.repaint();
                    if (scroll != null) {
                        scroll.repaint();
                    }
                }
            }

        }

        private Pair<Integer, Integer> contentBigInit(FileEntity file, JPanel childPanel, int y, int x) {
            int childWidth = 160;
            int childHeight = 200;
            int split = 30;
            int width = contentPanel.getWidth();
            if (x + childWidth + split > width) {
                x = split;
                y = y + childHeight + split;
            }
            childPanel.setLayout(null);
            childPanel.setBounds(x, y, childWidth, childHeight);

            String path = file.getPath();
            if (!file.isImage()) {
                path = file.getTypePath();
            }
            ImagePanel imagePanel = new ImagePanel(path);
            imagePanel.setBounds(15, 15, childWidth, 150);
            imagePanel.setVisible(true);
            childPanel.add(imagePanel);

            JLabel nameLabel = new JLabel(file.getFileName());
            nameLabel.setForeground(new Color(39, 101, 206));
            //设置组件鼠标手势
            nameLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
            //居中显示
            nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
            //设置组件长度
            int nameLength = SwingUtil.getFontWidth(childPanel.getGraphics(), new Font("微软雅黑", Font.PLAIN, 15), file.getFileName());
            if (nameLength > 150) {
                nameLength = 150;
            }
            nameLabel.setBounds(15, 160, 150, 40);
            //点击,打开文件夹
            nameLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    try {
                        Desktop.getDesktop().open(new File(file.getPath()));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            childPanel.add(nameLabel);

            contentPanel.add(childPanel);
            x += childWidth + split;
            return Pair.of(x, y);
        }

        private int contentDetailInit(FileEntity file, JPanel childPanel, int y) {
            childPanel.setLayout(null);
            childPanel.setBounds(10, y, contentPanel.getWidth() - 20, 60);
            childPanel.setBackground(new Color(245, 246, 247));

            String path = file.getPath();
            if (!file.isImage()) {
                path = file.getTypePath();
            }
            ImagePanel imagePanel = new ImagePanel(path);
            imagePanel.setBounds(5, 5, 60, 60);
            imagePanel.setVisible(true);
            childPanel.add(imagePanel);

            JLabel nameLabel = new JLabel(file.getFileName());
            nameLabel.setForeground(new Color(39, 101, 206));
            //设置组件长度
            int nameLength = SwingUtil.getFontWidth(childPanel.getGraphics(), new Font("微软雅黑", Font.PLAIN, 15), file.getFileName());
            if (nameLength > 250) {
                nameLength = 250;
            }
            nameLabel.setBounds(90, 20, nameLength, 20);
            nameLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
            //点击,打开文件夹
            nameLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    try {
                        Desktop.getDesktop().open(new File(file.getPath()));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            childPanel.add(nameLabel);


            String sizeTxt = file.getSize() + " B";
            if(file.getSize() > 1024 && file.getSize() < 1024 * 1024) {
                sizeTxt = (file.getSize() / 1000) + " KB";
            }if (file.getSize() > 1024 * 1024) {
                sizeTxt = (file.getSize() / 1024 / 1024) + " MB";
            }
            JLabel sizeLabel = new JLabel(sizeTxt);
            //设置组件长度
            int sizeLength = SwingUtil.getFontWidth(childPanel.getGraphics(), new Font("微软雅黑", Font.PLAIN, 15), sizeTxt);
            if (sizeLength > 100) {
                sizeLength = 100;
            }
            sizeLabel.setBounds(370, 20, sizeLength, 20);
            childPanel.add(sizeLabel);

            JLabel dateLabel = new JLabel(file.getLastModify());
            //设置组件长度
            dateLabel.setBounds(500, 20, 120, 20);
            childPanel.add(dateLabel);

            JLabel pathLabel = new JLabel(file.getPath());
            pathLabel.setForeground(new Color(39, 101, 206));
            //设置组件鼠标手势
            int pathLength = SwingUtil.getFontWidth(childPanel.getGraphics(), new Font("微软雅黑", Font.PLAIN, 15), file.getPath());
            if (pathLength > 300) {
                pathLength = 300;
            }
            //设置组件长度
            pathLabel.setBounds(650, 20, pathLength, 20);
            pathLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
            //点击,打开文件夹
            pathLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    try {
                        String absPath = file.getPath();
                        Desktop.getDesktop().open(new File(absPath.substring(0, absPath.lastIndexOf("\\"))));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            childPanel.add(pathLabel);

            JLabel copyLabel = new JLabel("复制");
            //设置组件长度
            copyLabel.setBounds(980, 20, 120, 20);
            copyLabel.setForeground(new Color(39, 101, 206));
            copyLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
            copyLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
            copyLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    //复制文件到剪贴板
                    File copy = new File(file.getPath());
                    FileToClipboard.copyFileToClipboard(copy);
                }
            });
            childPanel.add(copyLabel);

            contentPanel.add(childPanel);
            y = y + 70;
            return y;
        }
    }

}
